package com.ycsj.goldmedalnewconcept.master.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.adapter.EventAdapter;
import com.ycsj.goldmedalnewconcept.common.bean.EventInfo;
import com.ycsj.goldmedalnewconcept.common.bean.TeacherInfo;
import com.ycsj.goldmedalnewconcept.common.util.EventUtils;
import com.ycsj.goldmedalnewconcept.common.util.LpUtils;
import com.ycsj.goldmedalnewconcept.common.util.TeacherPoputil;
import com.ycsj.goldmedalnewconcept.common.util.TeacherUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 校长端活动 与家长端活动公用一套界面,老师端活动独立编写
 */

public class MasterEventActivity extends BaseActivity implements SrfManager.OnPullRefreshListener, OnloadingMoreListener, View.OnClickListener {

    //编辑
    @BindView(R.id.lv_edit)
    ImageView lvEdit;

    //标题
    @BindView(R.id.ll_title)
    LinearLayout llTitle;

    //下拉刷新
    @BindView(R.id.srf)
    SmartRefreshLayout srf;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    //列表控件
    @BindView(R.id.rv)
    RecyclerView rv;

    //加载框
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //内容视图
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    //数据集合
    private List<EventInfo.ListBean> oldDatas;

    //空视图
    private View emptyView;

    //数据适配器
    private EventAdapter adapter;

    //获取回调的老师数据
    private List<TeacherInfo.ListBean> teacherDatas;


    //记录选择老师弹框的位置,注意第一个是全部,不在数据源中
    private int choosePos = 0;

    //页面更新的标识
    public static final int UPDATE = 0x532;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_event;
    }

    @Override
    protected void init() {

        //状态栏
        initStateBar();

        //初始化视图
        initTitle();

        //初始化下拉刷新
        initSrf();

        //请求所有老师的接口
        httpTeacher();

    }


    /**
     * 初始化视图
     */
    private void initTitle() {
        tvTitle.setText("活 动");
        rlLoading.setVisibility(View.VISIBLE);
        rlContent.setVisibility(View.GONE);

        RelativeLayout.LayoutParams lp = LpUtils.getRelatLp(UiUtils.dp2px(36), UiUtils.dp2px(36));
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lvEdit.setLayoutParams(lp);
        lvEdit.setImageResource(R.drawable.common_class);
        lvEdit.setVisibility(View.VISIBLE);
    }

    /**
     * 设置监听
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }


    /**
     * 设置列表数据
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_white_bg, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("暂时没有新的活动内容");
        adapter = new EventAdapter(this, R.layout.parent_rv_event, oldDatas);

        //条目点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //整体条目的点击事件
                // 跳转到活动详情界面
                Intent intent = new Intent(MasterEventActivity.this, MaseterEventDetailsActivity.class);
                intent.putExtra("data", oldDatas.get(position));
                startActivity(intent);
            }
        });


    }

    /**
     * 请求所有老师的数据
     */
    private void httpTeacher() {
        TeacherUtils.httTeacher(rlLoading, this, new TeacherUtils.onSuccessListener() {
            @Override
            public void onSuccess(List<TeacherInfo.ListBean> datas) {
                //显示选择老师的按钮
                lvEdit.setVisibility(View.VISIBLE);

                //接受回调数据,获取所有的老师数据
                teacherDatas = datas;

                //在此数据源上添加一个默认全部的数据
                TeacherInfo.ListBean bean = new TeacherInfo.ListBean();
                bean.name = "全部";
                bean.isSelected = true;
                teacherDatas.add(0, bean);

                //设置列表适配器
                //初始化列表
                initRv();

                //请求页面数据,默认是请求全部的数据
                http(BqaManager.NORML, "");
            }

            @Override
            public void hasNoData() {
                //没有获取到老师数据不显示切换按钮
                lvEdit.setVisibility(View.GONE);
            }
        });
    }


    /**
     * 请求数据
     *
     * @param teacherId ""表示查看全部的活动,不为空时查看指定老师的活动,调用老师活动接口
     * @param method
     */
    private void http(int method, String teacherId) {

        EventUtils.httpEvent(this, method, "", teacherId, new EventUtils.OnRequestListener() {

            @Override
            public void onSuccess(int method, List<EventInfo.ListBean> newDatas) {
                if (method == BqaManager.NORML) {
                    if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                        rlLoading.setVisibility(View.GONE);
                    }
                    if (rlContent != null && rlContent.getVisibility() != View.VISIBLE) {
                        rlContent.setVisibility(View.VISIBLE);
                    }
                    oldDatas.clear();
                    oldDatas.addAll(newDatas);
                    BqaManager.setRv(emptyView, MasterEventActivity.this, adapter, rv, MasterEventActivity.this);
                } else {
                    //更新数据
                    BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                }
            }

            @Override
            public void onStateError(int method) {
                if (method == BqaManager.NORML) {
                    if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                        rlLoading.setVisibility(View.GONE);
                    }
                } else if (method == BqaManager.PULLREFRESH) {
                    //下拉刷新的时候关闭下拉刷新
                    if (srf != null) {
                        srf.finishRefresh(false);
                    }
                } else if (method == BqaManager.LOADINDGMORE) {
                    //加载更多的时候关闭加载更多
                    adapter.loadMoreComplete();
                }
            }

            @Override
            public void onError(int method) {
                BqaManager.setErrorNet(rlLoading, srf, method, adapter);
            }
        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH, teacherDatas.get(choosePos).userid);
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE, teacherDatas.get(choosePos).userid);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_edit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.lv_edit:
                //选择老师
                showChooseTeacehrPop();
                break;
        }
    }


    /**
     * 选择老师的弹框
     */
    private void showChooseTeacehrPop() {

        TeacherPoputil.showTeacherPop(this, teacherDatas, choosePos, llTitle, new TeacherPoputil.onPopItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                //记录当前点击条目的位置
                choosePos = pos;

                //请求页面数据
                http(BqaManager.PULLREFRESH, teacherDatas.get(choosePos).userid);
            }
        });
    }


    @Override
    public void busEvent(BusInfo busInfo) {
        if (null != busInfo && UPDATE == busInfo.type) {
            http(BqaManager.PULLREFRESH, teacherDatas.get(choosePos).userid);
        }
    }
}
